﻿Imports MindFusion.Diagramming
Imports MindFusion.Drawing

Public Class ClsMMContainerNode
    Inherits ContainerNode
    'represents my own diagram container

#Region "Attributes"
#Region "Private attributes"
    Private MiID As Integer = -1 'KROSLIB ID for this node. ID identifies this node, it is the same ID as in KrosLib - useful during synchroselection in compare version mode
    Private MsUnitName As String = "" 'UnitName from aMod DS = programmatic unit name. Coz Me.Caption = HumanUnitName = analytic unit name
    Private MbHasRTFnote As Boolean = False 'unit RTF note indicator
#End Region
#End Region
#Region "Constructors"
    Public Sub New()
        MyBase.new()
        MMSetDefaultProperties()
    End Sub

    Public Sub New(ByVal IiID As Integer)
        MyBase.new()
        MMSetDefaultProperties()
        MiID = IiID
    End Sub

    Public Sub New(ByVal prototype As MindFusion.Diagramming.ContainerNode)
        MyBase.New()
    End Sub

    Public Sub New(ByVal parent As MindFusion.Diagramming.Diagram)
        MyBase.New(parent)
        MMSetDefaultProperties()
    End Sub

    Protected Sub New(ByVal prototype As MindFusion.Diagramming.DiagramItem)
        MyBase.New(prototype)
    End Sub

    Protected Sub New(ByVal prototype As MindFusion.Diagramming.DiagramNode)
        MyBase.New(prototype)
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    Public Property UnitName() As String
        Get
            Return MsUnitName
        End Get
        Set(ByVal IsValue As String)
            MsUnitName = IsValue
        End Set
    End Property
    Public Property HasRTFnote() As Boolean
        Get
            Return MbHasRTFnote
        End Get
        Set(ByVal IbValue As Boolean)
            MbHasRTFnote = IbValue
        End Set
    End Property
    Public Property ID() As Integer
        Get
            Return MiID
        End Get
        Set(ByVal IiValue As Integer)
            MiID = IiValue
        End Set
    End Property
#End Region
#End Region
#Region "Subs"
#Region "Overrides & Overloads Subs"
    'overrided sub for saving to XML
    Protected Overrides Sub SaveToXml(ByVal xmlElement As System.Xml.XmlElement, ByVal context As MindFusion.Diagramming.XmlPersistContext)
        MyBase.SaveToXml(xmlElement, context)
        context.WriteString(CStr(MiID), "ID", xmlElement)
        context.WriteString(MsUnitName, "UnitName", xmlElement)
        context.WriteString(IIf(MbHasRTFnote, "TRUE", "FALSE"), "HasRTFnote", xmlElement)
    End Sub
    'overrided sub for loading from XML
    Protected Overrides Sub LoadFromXml(ByVal xmlElement As System.Xml.XmlElement, ByVal context As MindFusion.Diagramming.XmlPersistContext)
        MyBase.LoadFromXml(xmlElement, context)
        MiID = CInt(context.ReadString("ID", xmlElement))
        MsUnitName = context.ReadString("UnitName", xmlElement)
        MbHasRTFnote = IIf((context.ReadString("HasRTFnote", xmlElement)).ToUpper = "TRUE", True, False)
    End Sub
    'overrided sub for drawing a container
    Public Overrides Sub Draw(ByVal graphics As MindFusion.Drawing.IGraphics, ByVal options As MindFusion.Diagramming.RenderOptions)
        MyBase.Draw(graphics, options)

        'drawing triangle for container note indication
        If MbHasRTFnote Then DrawNoteTriangle(Me.Bounds, graphics)
    End Sub
    ''overrided sub for drawing handles
    'Public Overrides Sub DrawHandles(ByVal graphics As MindFusion.Drawing.IGraphics, ByVal color As System.Drawing.Color)
    '    'Const LdbWidthOfLine As Double = 0.7
    '    Dim LoColor As Color

    '    If Me.Parent.ActiveItem IsNot Nothing Then
    '        If Me.Parent.ActiveItem.Equals(Me) Then
    '            'me is active selected
    '            'Debug.Print("DrawHandles-SELECTED")
    '            LoColor = Drawing.Color.Blue
    '        Else
    '            'me is not active selected
    '            'Debug.Print("DrawHandles-NOT SELECTED")
    '            LoColor = Drawing.Color.Gray
    '        End If
    '    Else
    '        'nothing is selected
    '        'Debug.Print("DrawHandles-NOTHING IS SELECTED")
    '        LoColor = Drawing.Color.Gray
    '    End If

    '    MyBase.DrawHandles(graphics, LoColor)

    '    ''drawing selection rectangle
    '    'graphics.DrawRectangle(SDPenVirginCreator(LoColor, LdbWidthOfLine), CType(Me.Bounds.X - LdbWidthOfLine / 2, Single), CType(Me.Bounds.Y - LdbWidthOfLine / 2, Single), CType(Me.Bounds.Width + LdbWidthOfLine, Single), CType(Me.Bounds.Height + LdbWidthOfLine, Single))
    'End Sub
    'overrided sub called after deserialization of node. I can set some properties forcible here!
    Protected Overrides Sub OnLoad()
        MyBase.OnLoad()
    End Sub
#End Region
#Region "MM Subs"
    'default settings of shape
    Public Sub MMSetDefaultProperties()
        Me.EnabledHandles = AdjustmentHandles.All - AdjustmentHandles.Rotate
        Me.MinimumSize = New SizeF(20, 20) 'Constraints.MinWidth and Constraints.MinHeight dont work! :-)
        Me.Obstacle = True ' whether this node is considered an obstacle by the link-routing algorithm
        Me.Bounds = SDRectangleVirginCreator(0, 0, 50, 50)
        Me.Font = New Font("Arial", 8, FontStyle.Regular) 'font used when rendering the container's texts
        Me.AllowIncomingLinks = True 'true - because of notes
        Me.AllowOutgoingLinks = True 'true - because of notes
        Me.Brush = New LinearGradientBrush(Color.White, Color.LightBlue, 45) 'brush is used to fill the interiors of geometric shapes
        Me.Pen = New Pen("0/#FF696969/0/0/0//0/0/10/") 'pen is used to draw the outline of geometric shapes
        Me.HandlesStyle = HandlesStyle.SquareHandles2  'visual styles for frames that are drawn around selected nodes
        Me.ShadowOffsetX = 0.5 'horizontal offset of the shadow
        Me.ShadowOffsetY = 0.5 'vertical offset of the shadow
        Me.CaptionHeight = 6 'height of the container's caption area
        Me.Margin = 2 'how much space should be left between contained nodes and the container borders
        Me.AutoShrink = True 'whether the container should shrink in size when nodes are removed from it
        Me.Foldable = False 'whether the user is allowed to fold this container
        Me.HighlightPen = New Pen(Color.BlueViolet) 'pen used to draw the container frame lines when an item is dragged over the container
        Me.CaptionFormat.Alignment = StringAlignment.Center  'specifies the alignment of an object relative to its container
        Me.CaptionFormat.FormatFlags = StringFormatFlags.NoClip
        Me.CaptionFormat.Trimming = StringTrimming.None
        Me.CaptionFormat.LineAlignment = StringAlignment.Center
        Me.Caption = GsStringDefaultValue 'the text displayed as caption of the container
    End Sub
    'resizes height of caption and bounds
    Public Sub MMResizeAllToFitContainer()
        MMResizeHeightOfCaption()
        UpdateBounds()
    End Sub
    'resizes heights of caption
    Public Sub MMResizeHeightOfCaption()
        Dim LnNewHeight As Single
        Dim LnWidth As Single

        'checking Me.Parent for MeasureUnit
        If Me.Parent Is Nothing Then
            Debug.Print("CHYBA!!! - MMResizeHeightOfCaption - Me.Parent = Nothing")
        End If

        'resizing height of caption
        'maxWidth parameter for MeasureString sub = actual width of caption
        LnWidth = Me.Bounds.Width

        LnNewHeight = Me.Parent.MeasureString(Me.Caption, Me.Font, LnWidth, Me.CaptionFormat).Height

        'setting a new height or keeping minimal height of caption
        Const LnMinimalCaptionHeight As Single = 6
        Me.CaptionHeight = IIf(LnNewHeight > LnMinimalCaptionHeight, LnNewHeight, LnMinimalCaptionHeight)

        'height of container will be at minimum as height of caption + any constant (10 :-))
        Me.Constraints.MinHeight = Me.CaptionHeight + 10
        'need to set new height right now?
        If Me.Bounds.Height < Me.Constraints.MinHeight Then
            Me.Resize(Me.Bounds.Width, Me.Constraints.MinHeight)
        End If
    End Sub
    'routes links (depends on parameters - all incoming or all outgoing)
    Public Sub MMRouteLinks(Optional ByVal IbRouteIncomingLinks As Boolean = True, Optional ByVal IbRouteOutgoingLinks As Boolean = True)
        If IbRouteIncomingLinks Then
            For Each LoLink As DiagramLink In IncomingLinks
                LoLink.Route()
            Next
        End If

        If IbRouteOutgoingLinks Then
            For Each LoLink As DiagramLink In OutgoingLinks
                LoLink.Route()
            Next
        End If
    End Sub
    'adding node programmatically to container
    Public Sub MMAddNodeToContainerProgrammatically(ByVal OoNode As DiagramNode)
        'add node to container
        Me.Add(OoNode)
    End Sub
#End Region
#Region "Functions"
#Region "Overrides & Overloads Functions"
    'overrided sub - when a diagram item is being dragged over this node
    Public Overrides Function OnDragOver(ByVal item As MindFusion.Diagramming.DiagramItem) As Boolean
        'only table is accepted for adding to container
        If MMAllowAddNodeToContainer(item) Then
            Return MyBase.OnDragOver(item)
        Else
            Return False
        End If
    End Function
    'overrided sub - when a diagram item is dropped over this node
    Public Overrides Function OnDropOver(ByVal item As MindFusion.Diagramming.DiagramItem) As Boolean
        'only table is accepted for adding to container
        If MMAllowAddNodeToContainer(item) Then
            Return MyBase.OnDropOver(item)
        Else
            Return False
        End If
    End Function
#End Region
#Region "MM Functions"
    'examine possibility for adding node to container
    Private Function MMAllowAddNodeToContainer(ByVal IoItem As DiagramItem) As Boolean
        'only table is accepted for adding to container
        Return IoItem.GetType.Name = "ClsMMTableNode"
    End Function
#End Region
#End Region
#End Region

End Class
